package cn.com.chec.en.dao;

import cn.com.chec.en.model.domain.LanguageInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

@Repository
public class LanguageInfoDao {
	protected static final Logger log = Logger.getLogger(LanguageInfoDao.class);

	public boolean insert(LanguageInfo info) {
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("insert into tbl_language_i(lg_name,lg_desc) values(?,?)");
			pstmt.setString(1, info.getName());
			pstmt.setString(2, info.getDesc());
			pstmt.execute();
			conn.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<LanguageInfo> getAll() {
		List<LanguageInfo> list = new ArrayList<LanguageInfo>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("select * from tbl_language_i order by lg_id asc");
			ResultSet rs = pstmt.executeQuery();
			LanguageInfo info = null;
			while (rs.next()) {
				info = new LanguageInfo();
				info.setId(rs.getInt("lg_id"));
				info.setName(rs.getString("lg_name"));
				info.setDesc(rs.getString("lg_desc"));
				list.add(info);
			}
			pstmt.execute();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public LanguageInfo getLanguageInfoById(int id) {
		LanguageInfo languageInfo = new LanguageInfo();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("select * from tbl_language_i where lg_id = ?");
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				languageInfo.setId(rs.getInt("lg_id"));
				languageInfo.setName(rs.getString("lg_name"));
				languageInfo.setDesc(rs.getString("lg_desc"));
			}
			pstmt.execute();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return languageInfo;
	}

	public boolean update(LanguageInfo languageInfo) {
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("update tbl_language_i set lg_desc = ? where lg_id = ?");
			pstmt.setString(1, languageInfo.getDesc());
			pstmt.setInt(2, languageInfo.getId());
			pstmt.executeUpdate();
			conn.close();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Map<Integer, String> getLanuageMap() {
		Map<Integer, String> map = new HashMap<Integer, String>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("select * from tbl_language_i order by lg_id asc");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				map.put(Integer.valueOf(rs.getInt("lg_id")),
						rs.getString("lg_name"));
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}
}